import router from '@/router'
import {
    message
} from '@/utils/resetMessage';
export default {
    state: {
        tags: [],
        tabIndex: "home",
        menuIndex: 0,
        menu: [],
        showMenu: true,
        isCollapse: false, //折叠菜单
    },

    mutations: {
        setTags(state, data) {
            for (let index = 0; index < state.tags.length; index++) {
                if (data.name == state.tags[index].name) {
                    state.tags[index] = data
                    return
                }
            }
            state.tags.push(data)

        },
        setAllTags(state, data) {
            state.tags = data
        },
        setTabIndex(state, data) {
            state.tabIndex = data
        },
        setMenuIndex(state, data) {
            state.menuIndex = data
        },
        setCollapse(state, data) {
            state.isCollapse = data
        },
        setShowMenuStatus(state, data) {
            state.showMenu = data
        },
        setMenu(state, data) {
            state.menu = data
        },
        setMenuName(state, data) {
            state.tags.forEach(item => {
                if (item.name == data.path) {
                    item.title = data.value
                }
            })
        },
        
    },
    actions: {
        /**
         *
         *设置所有标签
         * @param {*} { commit }
         * @param {*} path
         */
        setAdminAllPageTag({
            commit,
            dispatch
        }, list) {
            commit('setAllTags', list)
        },
        setMenuList({
            commit,
            dispatch
        }, list) {
            commit('setMenu', list)
        },
        /**
         *
         *设置所有标签
         * @param {*} { commit }
         * @param {*} path
         */
        setMenuCollapse({
            commit
        }, data) {
            commit('setCollapse', data)
        },
        /**
         *
         *设置菜单是否显示
         * @param {*} { commit }
         * @param {*} path
         */
        setShowMenu({
            commit
        }, data) {
            commit('setShowMenuStatus', data)
        },
        /**
         *
         *添加标签
         * @param {*} { commit }
         * @param {*} path
         */
        setAdminPageTag({
            commit
        }, path) {
            commit('setTags', path)
        },
        /**
         *
         *设置标签选中项 
         * @param {*} { commit }
         * @param {*} active 标签name
         */
        setAdminPageTagActive({
            commit
        }, active) {
            commit('setTabIndex', active)
        },
        /**
         *
         *删除当前页标签
         */
        deleteCurrentPageTag({
            commit,
            state,
            dispatch
        }) {
            dispatch('deleteAdminPageTag', router.app._route.name)
        },
        /**
         *
         *删除单个标签
         * @param {*} { commit }
         * @param {*} targetName 删除的标签号
         * @return {*} 
         */
        deleteAdminPageTag({
            commit,
            state,
            dispatch
        }, targetName) {
            return new Promise((resolve, reject) => {
                let tabs = state.tags;
                let activeName = state.tabIndex;
                if (activeName === targetName) {
                    tabs.forEach((tab, index) => {
                        if (tab.name === targetName) {
                            let nextTab = tabs[index + 1] || tabs[index - 1];
                            if (nextTab) {
                                activeName = nextTab.name;
                            }
                        }
                    });
                }
                tabs = tabs.filter((tab) => tab.name !== targetName);
                dispatch('setAdminAllPageTag', tabs)
                resolve({
                    item: tabs[tabs.length - 1],
                    activeName
                })
            })
        },
        /**
         *
         *删除所有标签
         * @param {*} { commit }
         * @param {*} targetName 删除的标签号
         * @return {*} 
         */
        deleteAdminAllPageTag({
            commit,
            state,
            dispatch
        }) {
            return new Promise((resolve, reject) => {
                let tabs = []
                dispatch('setAdminAllPageTag', tabs)
                commit('setTabIndex', 'home')
                resolve()
            })
        },
        /**
         *
         *设置标签选中项 
         * @param {*} { commit }
         * @param {*} active 标签name
         */
        setAdminMenuActive({
            commit
        }, index) {
            commit('setMenuIndex', index)
        },
        setAdminMenuName({
            commit
        }, data) {
            commit('setMenuName', data)
        },
    }
}